<%@ page import="java.util.*" %>
<%@ page import="cn.edu.usst.cs.stu_sys011.*" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html lang="zh">
<head>
    <title>学生成绩系统</title>
    <meta charset="utf-8">
</head>
<body style="display: grid">
<form action="hello-servlet" method="post">
    <fieldset>
        <table>
            <%--学号，姓名，专业，平时，期末，总评--%>
            <thead>
            <tr>
                <th>学号</th>
                <th>姓名</th>
                <th>专业</th>
                <th>平时成绩</th>
                <th>期末成绩</th>
                <th>总成绩</th>
            </tr>
            </thead>
            <tbody>
            <%
                READ:{
                    Object infos_raw = request.getAttribute(HelloServlet.ATTR_TYPE);
                    if (!(infos_raw instanceof Set<?>)) {
                        System.out.println("Invalid Info Collection Raw");
                        break READ;
                    }
                    Set<?> infos = (Set<?>) infos_raw;
                    for (var a : infos) {
                        if (!(a instanceof StuInfo)) {
                            System.out.println("Invalid Info Raw");
                            break READ;
                        }
                        StuInfo info = (StuInfo) a;
                        String STU_MARK_ROUTINE = StuInfo.MARK_ROUTINE + "__" + info.stu_id();
                        String STU_MARK_FINAL = StuInfo.MARK_FINAL + "__" + info.stu_id();
                        String STU_MARK_TOTAL = StuInfo.MARK_TOTAL + "__" + info.stu_id();
                        String delete_class="delete__"+info.stu_id();
            %>
            <tr>
                <td><%=info.stu_id()%>
                </td>
                <td><%=info.name()%>
                </td>
                <td><%=info.major()%>
                </td>
                <%
                    var grade = info.grade();
                    float mark_routine = 0;
                    float mark_final = 0;
                    if (grade.get() != null) {
                        mark_routine = grade.getGrade_routine();
                        mark_final = grade.getGrade_final();
                    }
                    float mark_total = (grade.get() == null) ? 0 : grade.getGrade_total();
                %>
                <td>
                    <label for="<%=STU_MARK_ROUTINE%>">
                        <input id="<%=STU_MARK_ROUTINE%>"
                               name="<%=STU_MARK_ROUTINE%>"
                               value="<%=mark_routine%>">
                    </label>
                </td>
                <td>
                    <label for="<%=STU_MARK_FINAL%>">
                        <input id="<%=STU_MARK_FINAL%>"
                               name="<%=STU_MARK_FINAL%>"
                               value="<%=mark_final%>">
                    </label>
                </td>
                <td>
                    <%=mark_total%>
                </td>
                <td>
                    <button name="<%=delete_class%>" class="delete_buttons" >
                        删除<%=info.stu_id()%>
                    </button>
                </td>
            </tr>
            <% }
            } %>

            </tbody>

        </table>
        <label for="submit_grade_change_button" class="button">
            <input type="submit" id="submit_grade_change_button" name="submit_grade_change_button"
                   value="上传成绩">
        </label>
    </fieldset>

</form>

<form id="<%=HelloServlet.NEW_STU%>" style="display: none"
      action="hello-servlet" method="post">
    <label for="<%=StuInfo.STU_ID%>">
        学号
        <input id="<%=StuInfo.STU_ID%>">
    </label>
    <label for="<%=StuInfo.NAME %>">
        姓名
        <input id="<%=StuInfo.NAME %>">
    </label>
    <label for="<%=StuInfo.MAJOR %>">
        专业
        <input id="<%=StuInfo.MAJOR%>">
    </label>
</form>
<div>
    <button id="new_stu_row">
        新增学生信息
    </button>
</div>


<script type="text/javascript" src="main.js"></script>
</body>
</html>